package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.CooperativeFarmMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.AgriProductMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.Commodity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/cooperativeFarm")
public class CooperativeFarmController {
    @Resource
    CooperativeFarmMapper CooperativeFarmMapper;
    /**
     * 获取数据库全部数据
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(CooperativeFarmMapper.selectList(null));
    }

    /**
     * 分页获取数据库数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/pageAll")
    public Result<?> findPageC(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String tenantId) {
        LambdaQueryWrapper<CooperativeFarm> wrapper = Wrappers.<CooperativeFarm>lambdaQuery();
        wrapper.eq(CooperativeFarm::getTenantId, Integer.valueOf(tenantId));
        Page<CooperativeFarm> cooperativeFarmPage = CooperativeFarmMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(cooperativeFarmPage);
    }

    /**
     * 根据农场id添加信息
     * @param farmId
     * @param farmName
     * @param name
     * @param farmAdd
     * @param farmTel
     * @param farmPro
     * @param farmProfile
     * @param note
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveC(@RequestParam String farmId,
                           @RequestParam String farmName,
                           @RequestParam String name,
                           @RequestParam String farmAdd,
                           @RequestParam String farmTel,
                           @RequestParam String farmPro,
                           @RequestParam String farmProfile,
                           @RequestParam String note,
                           @RequestParam String tenantId) {
        if (farmId.isEmpty() || farmName.isEmpty() || name.isEmpty() || farmAdd.isEmpty() || farmTel.isEmpty()||farmPro.isEmpty()||farmProfile.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        CooperativeFarm cooperativeFarm = new CooperativeFarm(Integer.valueOf(farmId),farmName,name,farmAdd,farmTel,farmPro,farmProfile,note,Integer.valueOf(tenantId));
        CooperativeFarmMapper.insert(cooperativeFarm);
        return Result.success();
    }

    /**
     * 更新数据
     * @param farmId
     * @param farmName
     * @param name
     * @param farmAdd
     * @param farmTel
     * @param farmPro
     * @param farmProfile
     * @param note
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateById(@RequestParam String farmId,
                           @RequestParam String farmName,
                           @RequestParam String name,
                           @RequestParam String farmAdd,
                           @RequestParam String farmTel,
                           @RequestParam String farmPro,
                           @RequestParam String farmProfile,
                           @RequestParam String note,
                                @RequestParam String tenantId) {
        if (farmId.isEmpty() || farmName.isEmpty() || name.isEmpty() || farmAdd.isEmpty() || farmTel.isEmpty()||farmPro.isEmpty()||farmProfile.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        CooperativeFarm cooperativeFarm = new CooperativeFarm(Integer.valueOf(farmId),farmName,name,farmAdd,farmTel,farmPro,farmProfile,note,Integer.valueOf(tenantId));
        CooperativeFarmMapper.updateByMultiId(cooperativeFarm);
        return Result.success();
    }

   /* *//**
     * 商品溯源码查找
     * @param id
     * @return
     *//*

    @GetMapping("/findById")
    public Result<?> findById(@RequestParam("id") String id,
                              @RequestParam String tenantId) {
        CooperativeFarm cooperativeFarm = CooperativeFarmSurperMapper.selectById(id);
        List<CooperativeFarm> cooperativeFarmList = new ArrayList<>();
        if(cooperativeFarm != null)
        {
            cooperativeFarmList.add(cooperativeFarm);
            return Result.success(cooperativeFarmList);
        }
        return Result.error(0, "没有查询到相关数据");
    }*/

    /**
     * 根据农场名称模糊查找
     * @param farmName
     * @return
     */
    @GetMapping("/findByFarmName")
    public Result<?> findByFarmName(@RequestParam("farmName") String farmName,
                                    @RequestParam String tenantId) {
        LambdaQueryWrapper<CooperativeFarm> wrapper = Wrappers.<CooperativeFarm>lambdaQuery();
        wrapper.like(CooperativeFarm::getFarmName,farmName).eq(CooperativeFarm::getTenantId, tenantId);
        List<CooperativeFarm> cooperativeFarmList = CooperativeFarmMapper.selectList(wrapper);
        if(cooperativeFarmList.size() > 0)
            return Result.success(cooperativeFarmList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据负责人名称模糊查找
     * @param name
     * @return
     */
    @GetMapping("/findByName")
    public Result<?> findByName(@RequestParam("name") String name,
                                @RequestParam("tenantId") String tenantId) {
        LambdaQueryWrapper<CooperativeFarm> wrapper = Wrappers.<CooperativeFarm>lambdaQuery();
        wrapper.like(CooperativeFarm::getName,name).eq(CooperativeFarm::getTenantId, tenantId);
        List<CooperativeFarm> cooperativeFarmList = CooperativeFarmMapper.selectList(wrapper);
        if(cooperativeFarmList.size() > 0)
            return Result.success(cooperativeFarmList);
        return Result.error(0, "没有查询到相关数据");
    }


    /**
     * 删除

     * @return
     */
    @DeleteMapping("/delById")
    public Result<?> delete(@RequestParam("farmId") String farmId,
                            @RequestParam String tenantId) {
           CooperativeFarm cooperativeFarm=new CooperativeFarm();
        cooperativeFarm.setFarmId(Integer.valueOf(farmId));
        cooperativeFarm.setTenantId(Integer.valueOf(tenantId));
        int res = CooperativeFarmMapper.deleteByMultiId(cooperativeFarm);
        if(res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

}
